package com.kevms.phonewars;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

/** 
* @class PWLocationListener
* 
* @brief LocationListener used to listen for location changes.
* 
* This LocationListener registers with the location manager
* and handles location changes by sending location updates
* via the NetworkTask.
*/ 
public class PWLocationListener implements LocationListener {
	
	private static final String TAG = "PWLocationListener";
	private final Combat combat;
	public LocationManager locationManager;
	
	public PWLocationListener(Combat combat) {
		this.combat = combat;
		locationManager = (LocationManager)this.combat.getSystemService(Combat.LOCATION_SERVICE);
	}

	public void onLocationChanged(Location loc) {
		String s = "Location: " +
		"\n\tLatitude = " + loc.getLatitude() +
		"\n\tLongitude = " + loc.getLongitude() +
		"\n\tAltitude = " + loc.getAltitude();
		this.combat.tvScore.setText(s);
		
		this.combat.player.setPosition(loc.getLatitude(),loc.getLongitude(),loc.getAltitude());
		Log.d(TAG,"Trying to send position update");
		this.combat.removeNetworkTaskMessages(UpdateType.UPDATE_POSITION);
		this.combat.sendNetworkTaskMessage(UpdateType.UPDATE_POSITION);
		Log.d(TAG, "Sent location message to NetworkTask");
	}

	public void onProviderDisabled(String provider) {
		Toast.makeText( this.combat, "Gps Disabled", Toast.LENGTH_LONG ).show();
	}

	public void onProviderEnabled(String provider) {
		Toast.makeText( this.combat, "Gps Enabled", Toast.LENGTH_LONG ).show();
	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		
	}
	
	public void start() {
		//locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 250, 10, this);
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
	}
	
	public void stop() {
		locationManager.removeUpdates(this);
	}
}